package com.bw.cart.server.service.impl;

import com.bw.cart.common.domain.PmsComment;
import com.bw.cart.common.domain.vo.PmsPostsVo;
import com.bw.cart.server.mapper.CommentMapper;
import com.bw.cart.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<PmsPostsVo> selectPostsInfo() {
        List<PmsPostsVo> pmsPostsVos = commentMapper.selectPostsInfo();

        pmsPostsVos.forEach(pmsPostsVo -> {
            Long pmsPostsId = pmsPostsVo.getId();
            List<PmsComment> pmsComments = commentMapper.selectPmsComment(pmsPostsId, 0L);
            pmsPostsVo.setPmsCommentList(pmsComments);
            getCommentList(pmsComments, pmsPostsId);
        });
        return pmsPostsVos;
    }

    private void getCommentList(List<PmsComment> pmsComments, Long pmsPostsId) {
        pmsComments.forEach(pmsComment -> {
            Long commentId = pmsComment.getId();
            List<PmsComment> pmsComments1 = commentMapper.selectPmsComment(pmsPostsId, commentId);
            if (pmsComments1 != null && pmsComments1.size() > 0){
                getCommentList(pmsComments1,pmsPostsId);
            }
            pmsComment.setChildren(pmsComments1);
        });
    }
}
